package system

import (
	v1 "fiberx/api/system/v1"
	"fiberx/internal/utils"

	"github.com/gofiber/fiber/v3"
	"github.com/mssola/user_agent"
)

func (g *GuestInfo) GetGuestInfo(c fiber.Ctx) (res *v1.GuestInfoRes, err error) {
	// 获取客户端IP地址
	ipAddress := utils.IP.GetIpAddress(c)
	// 模拟地区信息
	region := utils.IP.GetIpSourceData(ipAddress)
	// 获取User - Agent
	userAgentStr := c.Get("User-Agent")
	// 解析User - Agent获取操作系统和浏览器信息
	ua := user_agent.New(userAgentStr)
	browser, _ := ua.Browser()
	os := ua.OS()

	// 创建GuestInfoRes结构体实例
	guestInfo := v1.GuestInfoRes{
		Ip:       ipAddress,
		Country:  region.Country,
		City:     region.City,
		Isp:      region.Isp,
		Province: region.Province,
		Os:       os,
		Browser:  browser,
	}

	return &guestInfo, nil
}
